<template>
  <!-- 库存详情页面 -->
  <div>
    <!-- 条件筛选 -->
    <el-row :gutter="24">
      <el-col :span="24">
        <el-card shadow="always">
          <el-form :inline="true" :model="queryForm" ref="queryForm" class="demo-form-inline">
            <el-form-item label="商品" prop="product">
              <el-input v-model="queryForm.product" placeholder="商品名/条码"></el-input>
            </el-form-item>
            <el-form-item label="库存状态" prop="inventoryStatus">
              <el-select v-model="queryForm.inventoryStatus" placeholder="库存状态">
                <el-option label="负库存" value=1></el-option>
                <el-option label="0库存" value=2></el-option>
                <el-option label="超出预警上限" value=3></el-option>
                <el-option label="低于预警下限" value=4></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="warning" @click="resetForm('queryForm')">重置</el-button>
              <el-button type="primary" @click="onSubmit">查询</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>

    <br>

    <!-- 按钮操作 -->
    <el-row :gutter="24">
      <el-col :span="24">
        <el-card shadow="always" style="text-align: left">
          <el-button type="primary" size="small" @click="addInventory">商品入库</el-button>
          &nbsp;&nbsp;&nbsp;&nbsp;
          <span class="title">可用库存：</span><span class="count">1234</span>
          &nbsp;&nbsp;&nbsp;&nbsp;
          <span class="title">锁定库存：</span><span class="count">2345</span>
          &nbsp;&nbsp;&nbsp;&nbsp;
          <span class="title">总库存：</span><span class="count">3579</span>
          &nbsp;&nbsp;&nbsp;&nbsp;
          <span class="title">批量预警设置：</span>
          <el-select v-model="waringForm.setType" placeholder="批量预警设置" size="mini" style="width: 133px">
            <el-option label="选中设置" value=0></el-option>
            <el-option label="筛选设置" value=1></el-option>
          </el-select>
        </el-card>
      </el-col>
    </el-row>

    <br>

    <!-- 数据列表 -->
    <el-row :gutter="24">
      <el-col :span="24">
        <el-card shadow="always">
          <el-table :data="tableData" border style="width: 100%" @selection-change="handleSelectionChange"
                    @sort-change="sortChange">
            <el-table-column type="selection" width="50"></el-table-column>
            <el-table-column prop="productName" label="商品" width="320" sortable="custom"></el-table-column>
            <el-table-column prop="unit" label="单位"></el-table-column>
            <el-table-column prop="usableInventory" label="可用库存"></el-table-column>
            <el-table-column prop="lockInventory" label="锁定库存"></el-table-column>
            <el-table-column prop="inventoryMax" label="库存预警上限"></el-table-column>
            <el-table-column prop="inventoryMin" label="库存预警下限"></el-table-column>
            <el-table-column label="操作" width="130">
              <template slot-scope="scope">
                <el-button type="text" size="mini" @click="setWarning(scope.row.id)">预警设置</el-button>
                <el-button type="text" size="mini" @click="inventoryFlowingDetail(scope.row.productId)">流水</el-button>
                <el-button type="text" size="mini">分布</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="block" style="margin-top: 10px; text-align: right">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="[10, 20, 30, 50]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="dataCount">
            </el-pagination>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 商品导入 -->
    <el-dialog title="预警设置" :visible.sync="dialogFormVisible" :before-close="closeSetWarningBefore" width="30%">
      <el-form style="text-align: left;" :model="waringForm" :rules="rules" ref="waringForm"
               label-width="100px">
        <el-form-item label="预警上限" prop="inventoryMax" style="width: 300px;">
          <el-input v-model.number="waringForm.inventoryMax"></el-input>
        </el-form-item>
        <el-form-item label="预警下限" prop="inventoryMin" style="width: 300px;">
          <el-input v-model.number="waringForm.inventoryMin"></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="closeSetWarning">取 消</el-button>
          <el-button type="primary" @click="saveSetWarning">确 定</el-button>
        </span>
    </el-dialog>


  </div>

</template>

<script>
  import {post} from '@/api/http.js'

  export default {
    name: "inventoryDetails",

    // 页面加载就执行
    mounted: function () {
      this.getListData();
    },

    data() {
      return {
        // 表单数据邦迪
        queryForm: {
          product: '',
          inventoryStatus: '',
          current: 0,
          size: 10,
          sortBy: '',
          sortOrder: ''
        },

        // 表格数据
        tableData: [],

        // 列表多选数据ID集合
        selectIds: [],

        // 分页参数
        currentPage: 1,
        pageSize: 10,
        dataCount: 0,

        // 预警设置弹出框
        dialogFormVisible: false,
        formLabelWidth: '120px',
        specification: '',
        waringForm: {
          ids: [],
          query: this.queryForm,
          inventoryMax: null,
          inventoryMin: null,
          setType: null
        },
        rules: {
          inventoryMax: [
            {required: true, message: '请输入预警上限', trigger: 'blur'},
            {type: 'number', message: '请填写数字格式'},
          ],
          inventoryMin: [
            {required: true, message: '请输入预警下限', trigger: 'blur'},
            {type: 'number', message: '请填写数字格式'},
          ]
        },
      }
    },

    // 方法区
    methods: {

      // 单个库存信息详细流水
      inventoryFlowingDetail(productId){
        this.$store.commit("setProductId", productId);
        this.$router.push('/inventory/flowing');
      },

      // 关闭预警设置对话框之前的回调
      closeSetWarningBefore(done) {
        this.resetForm('waringForm');
        done();
      },
      closeSetWarning(){
        this.resetForm('waringForm');
        this.dialogFormVisible=false;
      },

      // 保存预警设置
      saveSetWarning(){
        this.waringForm.setType = 0;
        post("/inventory/warning", this.waringForm)
          .then(res =>{
            this.$message({
              message: '商品预警设置成功',
              type: 'success'
            });
            this.getListData();
            this.closeSetWarning();
          })
          .catch(err =>{})
          .finally(() =>{})
      },

      // 预警设置弹出框
      setWarning(id) {
        let ids = [];
        ids[0] = id;
        this.waringForm.ids = ids;
        this.dialogFormVisible = true;
      },

      // 商品入库
      addInventory() {
        this.$router.push("/inventory/put");
      },

      // 表格数据多选,获取ID
      handleSelectionChange(val) {
        // val 传给的是数组，里面放的是选中当前行的所有数据
        // console.info(val);
        // 声明一个空数组，将val遍历获取每一行数据的ID，放入集合
        let idsArr = [];
        val.forEach(row => {
          idsArr.push(row.id)
        });
        // 赋值给全局变量
        this.selectIds = idsArr;
      },

      // 表格排序
      sortChange(column) {
        this.queryForm.sortBy = column.prop;
        this.queryForm.sortOrder = column.order;
        this.getListData();
      },

      // 提交查询表单
      onSubmit() {
        this.getListData();
      },

      // 重置查询表单
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },

      // 分页组件参数变动
      handleSizeChange(val) {
        this.queryForm.size = val;
        this.getListData();
      },
      handleCurrentChange(val) {
        this.queryForm.current = val;
        this.getListData();
      },

      // 获取列表数据
      getListData() {
        post("/inventory/list", this.queryForm)
          .then(res => {
            this.tableData = res.data.records;
            this.dataCount = res.data.total;
            this.pageSize = res.data.size;
            this.currentPage = res.data.current;
          })
          .catch(err => {
          })
          .finally(() => {
          })
      }
    }
  }
</script>

<style scoped>
  .title {
    font-size: 12px;
  }

  .count {
    font-size: 12px;
    color: #ff0000;
  }

  .el-dialog {
    width: 388px;
  }
</style>
